/*
 * Copyright 2011 myTDev.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mytdev.javafx.internal.scene.control.behavior;

import com.mytdev.javafx.scene.control.ImageChooser;
import com.sun.javafx.scene.control.behavior.BehaviorBase;
import java.io.File;
import java.net.MalformedURLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.image.Image;
import javafx.stage.FileChooser;

/**
 *
 * @author Yann D'Isanto
 */
public class ImageChooserBehavior extends BehaviorBase<ImageChooser> {

    private static final Logger LOG = Logger.getLogger(ImageChooser.class.getName());

    private static final FileChooser FILE_CHOOSER = new FileChooser();

    static {
        FILE_CHOOSER.getExtensionFilters().add(
                new FileChooser.ExtensionFilter("Images files (*.jpg, *.png, *.gif, *.bmp)",
                "*.jpg", "*.png", "*.gif", ".bmp"));
    }
    /**
     * Clear image chooser event handler.
     */
    private final EventHandler<ActionEvent> clearEventHandler = new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            getControl().setSelectedImage(null);
        }
    };

    /**
     * Browse files event handler.
     */
    private final EventHandler<ActionEvent> browseEventHandler = new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            File file = FILE_CHOOSER.showOpenDialog(getControl().getScene().getWindow());
            if (file != null) {
                try {
                    getControl().setSelectedImage(
                            new Image(file.toURI().toURL().toString(), false));
                } catch (MalformedURLException ex) {
                    LOG.log(Level.SEVERE, null, ex);
                }
            }
        }
    };

    /**
     * @see BehaviorBase#BehaviorBase(javafx.scene.control.Control)
     */
    public ImageChooserBehavior(ImageChooser imageChooser) {
        super(imageChooser);
    }

    /**
     * Returns the clear image selection event handler
     *
     * @return an EventHandler instance.
     */
    public EventHandler<ActionEvent> getClearEventHandler() {
        return clearEventHandler;
    }

    /**
     * Returns the browse files event handler
     *
     * @return an EventHandler instance.
     */
    public EventHandler<ActionEvent> getBrowseEventHandler() {
        return browseEventHandler;
    }
}
